package com.lxc.lib.net.storage

/**
 *@author: xuancai.Liu
 *@create: 2021-09-06 17:37
 *@program: NetDemo
 *@description:
 */
class BaseResponse<T> private constructor(
    var code: Int = 1,
    var status: Boolean,
    var msg: String? = null,
    var result: T? = null
) {
    companion object {
        fun <T> success(data: T?) = BaseResponse(status = true, result = data)

        fun <T> failure(code: Int = -1, msg: String? = null) =
            BaseResponse<T>(status = false, code = code, msg = msg)
    }

    inline fun onSuccess(onSuccess: (BaseResponse<T>) -> Unit): BaseResponse<T> {
        if (!status) return this
        onSuccess(this)
        return this
    }

    inline fun onFailure(onFailure: (BaseResponse<T>) -> Unit): BaseResponse<T> {
        if (status) return this
        onFailure(this)
        return this
    }

    override fun toString(): String {
        return "NetworkResult(state=$status, data=$result, code=$code, msg=$msg)"
    }
}